//
//  COReportViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/9/6.
//

import UIKit

class COReportViewController: COBaseViewController {
    private var animationDelegate: COPresentAnimationDelegate = COPresentAnimationDelegate()
    private var contents: [String] = ["Sexual content", "Violent content", "Scary content", "Objectionable content", "Other"]
    var showVc: UIViewController?
    private var selectText = "Sexual content"
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
        self.modalPresentationStyle = .custom
        self.transitioningDelegate = animationDelegate
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override var navShow: Bool {
        false
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func configUI() {
        view.backgroundColor = .clear
        view.addSubview(mainView)
        mainView.addSubview(backImageView)
        mainView.addSubview(bgView)
        mainView.addSubviews([tableView, nameLabel, closeButton, tableView, okButton])
        
        mainView.snp.makeConstraints { make in
//            make.left.right.bottom.equalToSuperview()
            make.edges.equalToSuperview()
            make.height.equalTo(547+bottomSafeHeight)
        }
        
        backImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(-1)
            make.top.equalToSuperview().offset(0)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(backImageView.snp.bottom)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(33)
        }
        
        closeButton.snp.makeConstraints { make in
            make.right.top.equalToSuperview().inset(15)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(nameLabel.snp.bottom).offset(30)
            make.bottom.equalTo(okButton.snp.top).offset(-30)
        }
        
        okButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(23)
            make.bottom.equalToSuperview().offset(-15-bottomSafeHeight)
            make.height.equalTo(50)
        }
    }

    private lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    private lazy var backImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "矩形"))
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.font = .medium(25)
        label.text = "Report"
        return label
    }()
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(17, 7, 41, 1)
        return view
    }()
    
    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.delegate = self
        tableV.dataSource = self
        tableV.separatorStyle = .none
        tableV.register(cellWithClass: COReportTableViewCell.self)
        tableV.backgroundColor = .rgba(17, 7, 41, 1)
        return tableV
    }()
    
    private lazy var closeButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "编组 27"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var okButton: UIButton = {
        let btn = UIButton()
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor.rgba(218, 67, 255).cgColor, UIColor.rgba(82, 28, 255).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: 0, y: 0, width: SCREENWIDTH-38, height: 50)
        bgLayer1.startPoint = CGPoint(x: 1, y: 0.56)
        bgLayer1.endPoint = CGPoint(x: 0.56, y: 0.56)
        btn.layer.insertSublayer(bgLayer1, at: 0)
        
        btn.setTitle("Report", for: .normal)
        btn.titleLabel?.font = .medium(14)
        btn.setTitleColor(.white, for: .normal)
        
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 25
        
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
}

extension COReportViewController: UITableViewDelegate, UITableViewDataSource {
    @objc
    private func clickButton(_ btn: UIButton) {
        _ = ""
        if btn == closeButton {
            self.dismiss(animated: true)
            return
        } else if btn == okButton {
            self.view.makeToastActivity(.center)
            
            DispatchQueue.main.asyncAfter(deadline: .now()+2, execute: {
                self.view.hideToastActivity()
                self.view.makeToast("If the report is successful, we will delete the improper content within 24 hours and punish the user", position: .center) { _ in
                    self.dismiss(animated: true)
                }
            })
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: COReportTableViewCell.self)
        let text = contents[indexPath.row]
        cell.button.setTitle(text, for: .normal)
        cell.button.isSelected = text == selectText
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        75
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectText = contents[indexPath.row]
        tableView.reloadData()
    }
}
